<template>
  <div class="addform">
    <h1>工作经历详情</h1>
    <br />
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-position="right"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="公司名称:" prop="weName">
        <el-input v-model="ruleForm.weName" class="inputS"></el-input>
      </el-form-item>
      <el-form-item label="所在城市:" prop="weCity">
        <el-input v-model="ruleForm.weCity" class="inputS"></el-input>
      </el-form-item>
      <el-form-item label="自我评价:" prop="weSeva">
        <el-input v-model="ruleForm.weSeva" class="inputS"></el-input>
      </el-form-item>
      <el-form-item label="起止时间:" prop="weStart">
        <el-date-picker
          v-model="ruleForm.weStart"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
        <i class="el-icon-minus"></i>
        <el-date-picker
          v-model="ruleForm.weEnd"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="内容:" prop="weCon">
        <el-input
          type="textarea"
          v-model="ruleForm.weCon"
          :rows="6"
          class="inputL"
        ></el-input>
      </el-form-item>
      <!-- 图片 -->
      <el-form-item label="图片:">
        <el-image
          class="imgPreview"
          :src="this.imgUrl + ruleForm.weImg"
          fit="cover"
          @click="Pview(ruleForm.weImg)"
          title="浏览大图"
        ></el-image>
        <el-upload
          :action="this.uploadIndex3"
          list-type="picture-card"
          ref="upload"
          :auto-upload="false"
          :on-change="beforeAvatarUpload"
          :on-success="handleAvatarSuccess"
        >
          <i class="el-icon-plus"></i>
          <div class="el-upload__tip" slot="tip">
            只能上传jpg/png文件，且不超过20mb
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >修改信息</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 修改成功提示 -->
    <el-dialog
      title="温馨提示"
      :visible.sync="successVisible"
      width="20%"
      @close="resetForm(ruleForm)"
      append-to-body
    >
      <div class="dialogSuc">
        <el-button
          type="success"
          icon="el-icon-check"
          circle
          @click="successVisible"
        ></el-button>
        <h2>修改成功,3秒后跳转</h2>
      </div>
    </el-dialog>
    <!-- 图片预览 -->
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      successVisible: false,
      ruleForm: {},
      rules: {
        //如果没输入的提示信息
        weName: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { max: 30, message: "最大长度30个字", trigger: "blur" },
        ],
        weCity: [
          { required: true, message: "请输入城市", trigger: "blur" },
          { max: 20, message: "最大长度20个字", trigger: "blur" },
        ],
        weSeva: [
          { required: true, message: "请输入自我评价", trigger: "blur" },
          { max: 50, message: "最大长度50个字", trigger: "blur" },
        ],
        weStart: [{ required: true, message: "请选择日期", trigger: "blur" }],
        weEnd: [{ required: true, message: "请选择日期", trigger: "blur" }],
      },
    };
  },
  created() {
    // 提交方法 先判断提交表单是否有空=>validate
    console.log("进入修改个人信息页面");
    const _this = this;
    console.log(_this.$route.params.Id);
    this.axios({
      method: "get",
      url: _this.baseUrl + "/index3/get/workExpOne/" + _this.$route.params.Id,
    }).then(function (resp) {
      console.log(resp.data);
      _this.ruleForm = resp.data
      let date1 = _this.ruleForm.weStart + ""; //转换成字符型
      _this.ruleForm.weStart = date1.substr(0, 10);
      let date2 = _this.ruleForm.weEnd + ""; //转换成字符型
      _this.ruleForm.weEnd = date2.substr(0, 10);
    });
  },
  methods: {
    // 预览 无需管
    Pview(file) {
      file=this.imgUrl+file;
      console.log(file);
      this.dialogImageUrl = file;
      this.dialogVisible = true;
    },
    // 上传图片判断
    beforeAvatarUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 10;
      console.log("文件大小:" + (file.size / 1024 / 1024).toFixed(2) + "MB");
      if (!isLt10M) {
        this.$message.error("上传图片大小不能超过 10MB!");
        this.$refs.upload.clearFiles();
      } else {
        console.log(file);
        this.$refs.upload.submit();
      }
      return isLt10M;
    },
    //上传
    handleAvatarSuccess(res, file) {
      this.ruleForm.weImg = "/assets/myIndex3/img/" + file.name;
      console.log("图片:" + this.ruleForm.weImg);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 由于axios内部必须使用_this才能使用this
          const _this = this;
          this.axios({
            method: "post",
            url: _this.baseUrl + "/index/post/editWorkExp",
            //传递给后端的数据
            data: _this.ruleForm
          }).then(function (resp) {
            console.log(resp.data);
            _this.successVisible = true;//提交成功提示
            // 修改成功后3秒跳转
            setTimeout(function () {
              _this.$router.push("/main/index3WorkExp");
            }, 3000);
          });
        } else {
          console.log("修改失败");
          return false;
        }
      });
    },
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.addform {
  width: 80%;
  margin: 0 auto;
  .inputS {
    width: 80%;
  }
  .inputL {
    width: 80%;
    display: block;
  }
  .imgPreview {
    width: 150px;
    height: 150px;
    margin-right: 15px;
    float: left;
    cursor: pointer;
  }
}
.dialogSuc {
  display: block;
  margin: 0 auto;
  width: 50%;
  button {
    display: block;
    margin: 0 auto;
  }
  h2 {
    color: green;
    text-align: center;
  }
}
</style>